package cn.yusei.teamTribe.controller.team;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.yusei.teamTribe.constant.ResponseCode;
import cn.yusei.teamTribe.entity.TeamAlbum;
import cn.yusei.teamTribe.service.team.TeamAlbumService;
import cn.yusei.teamTribe.util.ResponseUtil;

@Controller
public class TeamAlbumController {

	@Autowired
	private TeamAlbumService taService;
	
	@RequestMapping(value="/teamAlbum/add")
	public void add(@RequestAttribute int id, TeamAlbum ta, PrintWriter out) {
		if(!ta.isValid()) {
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		int code = 0;
		int albumId = 0;
		try {
			code = taService.add(id, ta);
			if(code == ResponseCode.REQUEST_SUCCESSED)
				albumId = taService.getLastInsertId();
		}
		catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		Map<String, Object> param = new HashMap<String, Object>();
		if(code == ResponseCode.REQUEST_SUCCESSED)
			param.put("albumId", albumId);
		ResponseUtil.response(out, code,  param);
	}
	
	@RequestMapping(value="/teamAlbum/delete")
	public void delete(@RequestAttribute int id, @RequestParam int teamId, @RequestParam int albumId, PrintWriter out) {
		if(teamId <= 0 || albumId <= 0) {
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		int code = 0;
		try {
			code = taService.delete(id, teamId, albumId);
		}
		catch (Exception e) {
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		ResponseUtil.responseCode(out, code);
	}
	
	@RequestMapping(value="/teamAlbum/getById")
	public void getById(@RequestParam int albumId, PrintWriter out) {
		if(albumId <= 0) {
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		TeamAlbum tpa = null;
		try {
			tpa = taService.getById(albumId);
		}
		catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("album", tpa);
		ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);
	}
	
	@RequestMapping(value="/teamAlbum/getByTeam")
	public void getByTeam(@RequestParam int teamId, PrintWriter out) {
		if(teamId <= 0) {
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		int[] ids = null;
		try {
			ids = taService.getByTeamId(teamId);
		}
		catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("ids",  ids);
		ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);
	}
	
	@RequestMapping(value="/teamAlbum/update")
	public void update(@RequestAttribute int id, TeamAlbum tpa, PrintWriter out) {
		if(tpa.getId() <= 0 || !tpa.isValid()) {
			ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
			return;
		}
		int code = 0;
		try {
			code = taService.update(id, tpa);
		}
		catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
			return;
		}
		ResponseUtil.responseCode(out, code);
	}
}
